package com.zkca.xf.ui.activity.webview;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.zkca.xf.R;
import com.zkca.xf.base.BaseTitleActivity;
import com.zkca.xf.utils.LogUtil;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MyWebViewActivity extends BaseTitleActivity {


    @BindView(R.id.myweb)
    WebView myweb;
    @BindView(R.id.progressBar1)
    ProgressBar pg1;

    private String title, url;
    Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_web_view);
        ButterKnife.bind(this);

        intent = getIntent();
        title = intent.getStringExtra("title");
        url = intent.getStringExtra("url");
        initMyView();
        initView();
        initdata();
    }

    private void initView() {
        myweb.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        myweb.getSettings().setLoadWithOverviewMode(true);
        myweb.getSettings().setJavaScriptEnabled(true);
        myweb.getSettings().setUseWideViewPort(true);
        myweb.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        myweb.getSettings().setLoadWithOverviewMode(true);
        //如果不设置WebViewClient，请求会跳转系统浏览器
        myweb.setWebViewClient(new WebViewClient() {

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }

            public boolean shouldOverrideUrlLoading(WebView view, String url) {

               // view.loadUrl(url);
                return true;

            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
            }

        });

        //设置经度条
        myweb.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                // TODO 自动生成的方法存根

                if (newProgress == 100) {
                    pg1.setVisibility(View.GONE);//加载完网页进度条消失
                } else {
                    pg1.setVisibility(View.VISIBLE);//开始加载网页时显示进度条
                    pg1.setProgress(newProgress);//设置进度值
                }

            }
        });
    }

    private void initdata() {
        // id = getIntent().getStringExtra("id");
        LogUtil.e("lcq", url);
        myweb.loadUrl(url);
    }

    private void initMyView() {

        setTitleText(title);
        setleftTextVisibility(true);
        setleftImgVisibility(true);

    }
}
